﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web.Caching;
using System.Web;
using System.Collections;
using DocManagerORM.ORM;

namespace DocManagerORM.Caching
{
   public  class CacheBase
    {
                
      protected static Dictionary<string,IRefresh> _DictCache = new Dictionary<string,IRefresh>(5)  ; 

      private static Cache _cache = getCache();

      public CacheBase()
       {
           if(_cache == null)_cache = getCache();
       }
              
      public static Cache CacheDati
        {
            get { return _cache; }     
        }
                   
      private static Cache getCache()
       {
           HttpContext context = HttpContext.Current;
           if (context != null)
           {
               return context.Cache;
           }
           else
           {
               return HttpRuntime.Cache;
           }

       }//fM

      public static IList<T> getDati<T>(string pKey)
       {
           return CacheDati.Get(pKey) as IList<T>;       
       }//fM

      public static IList getDati(string pKey)
      {
          return CacheDati.Get(pKey) as IList;
      }//fM
       
      public static void BindaComboBoxdata<T>(ref System.Windows.Controls.ComboBox pCombo, string pKey, string pValueField, 
           string pDisplayField,int? pSelector=null)
       {

           pCombo.ItemsSource = null;
           pCombo.Items.Clear();          

           pCombo.DisplayMemberPath = pDisplayField;
           pCombo.SelectedValuePath = pValueField;
           if (pSelector.HasValue)
           {
               var res = CacheDati.Get(pKey) as ILookup<int, T>;
               if (res == null) return;
               pCombo.ItemsSource = res[pSelector.Value];

           }
           else
               pCombo.ItemsSource = CacheDati.Get(pKey) as IList<T>;
       }



      public static void BindaComboBoxdataPredefinito(ref System.Windows.Controls.ComboBox pCombo, string pKey,int? pSelector = null)
      {

          pCombo.ItemsSource = null;
          pCombo.Items.Clear();

          switch (pKey.ToLower())
	    {
              case "categorie":
                pCombo.DisplayMemberPath = "Categoria";
                pCombo.SelectedValuePath = "ID";
                pCombo.ItemsSource = CacheDati.Get(pKey) as IList<Categorie>;
                break;
              case "sottocategorie":
                pCombo.DisplayMemberPath = "Categoria";
                pCombo.SelectedValuePath = "ID";
                pCombo.ItemsSource = CacheDati.Get(pKey) as IList<SottoCategorie>;
                break;
              case "aspnet_roles":
                pCombo.DisplayMemberPath = "RoleName";
                pCombo.SelectedValuePath = "RoleID";
                pCombo.ItemsSource = CacheDati.Get(pKey) as IList<aspnet_Roles>;
                break;
              case "tags":
                pCombo.DisplayMemberPath = "TagsPre";
                pCombo.SelectedValuePath = "ID";
                pCombo.ItemsSource = CacheDati.Get(pKey) as IList<Tags>;
                break;
             default:
                return;
                break;
	    }    

              
      }//fM
     
      public static void InitializeCachedati(int pMinutiDiValidità)
       {

           try
           {

           CacheDati<IList<Categorie>> cacheCat = new CacheDati<IList<Categorie>>(
               () => { return new QueryCodifiche().getCATEGORIE(null).ToList(); },
               "Categorie", pMinutiDiValidità, System.Web.Caching.CacheItemPriority.High);
              

           CacheDati<ILookup<int, SottoCategorie>> cacheSottoCat = new CacheDati<ILookup<int, SottoCategorie>>(
              () => { return new QueryCodifiche().getSottoCATEGORIE(null).ToLookup(sc => sc.Fk_IDCat, sc => sc); },
                 "SottoCategorie", pMinutiDiValidità, System.Web.Caching.CacheItemPriority.High);


           CacheDati<IList<SottoCategorie>> cacheSottoCatTutte = new CacheDati<IList<SottoCategorie>>(
             () => { return new QueryCodifiche().getSottoCATEGORIE(null).ToList(); },
                "SottoCategorieTutte", pMinutiDiValidità, System.Web.Caching.CacheItemPriority.High);

           CacheDati<IList<aspnet_Roles>> cacheRuoli = new CacheDati<IList<aspnet_Roles>>(
              () => { return new ORM.QueryCodifiche().getRUOLI().ToList(); },
              "aspnet_Roles", pMinutiDiValidità, System.Web.Caching.CacheItemPriority.High);

           CacheDati<IList<Tags>> cacheTags = new CacheDati<IList<Tags>>(
            () => { return new ORM.QueryCodifiche().getTagsPredefiniti(null).ToList(); },
            "Tags", pMinutiDiValidità, System.Web.Caching.CacheItemPriority.High);

           }
           catch (Exception ex)
           {
               throw;               
           }


       }//fM


      public static void RefreshCacheDati(string pKeyCache)
      {
         var cac = _DictCache[pKeyCache] as IRefresh;
         if(cac != null) cac.Refresh(); 
          
      }//fM


    }//fC


   
}
